// 获取政治面貌
import { formatTreeData, getStatusMap } from '@/services/common';
import { getByScope } from '@/services/deptControl';
import _ from 'lodash';

export const getPolitical = async () => {
  const res = await getStatusMap('uc_employee_political');
  return res?.code === 200 ? res.data : [];
};

// 获取职务
export const getJob = async () => {
  const res = await getStatusMap('uc_employee_job');
  return res?.code === 200 ? res.data : [];
};

// 获取岗位
export const getPosition = async () => {
  const res = await getStatusMap('uc_employee_position');
  return res?.code === 200 ? res.data : [];
};

// 获取状态
export const getStatus = async () => {
  const res = await getStatusMap('uc_employee_status');
  return res?.code === 200 ? res.data : [];
};

// 获取部门树
export const getDepartmentTreeByRole = async (params: any) => {
  const res = await getByScope(params);
  return formatTreeData(res?.data);
};

//移除掉空值
export const filterEmptyValueForObj = (obj: Record<string, unknown>) => {
  const newObj: any = {};
  for (const item in obj) {
    if (obj[item]) {
      newObj[item] = obj[item];
    }
  }

  return newObj;
};

//座机号码校验
export const telephone = (value: string | void): string => {
  if (!value) return '';
  const is: boolean = /^1(3|4|5|6|7|8|9)\d{9}$/.test(value);
  return is ? '' : '请输入正确的手机号码';
};

export const email = (value: string): string => {
  if (!value) return '';
  const is: boolean =
    /^([a-z0-9_\\.-]+)@([\da-zA-Z\\.-]+)\.([a-zA-Z\\.]{2,6})$/.test(value);
  return is ? '' : '请输入正确的邮箱地址';
};

export const formatLimitTree = (treeData: any) => {
  const data = _.cloneDeep(treeData);
  const newData = [];
  if (Array.isArray(data) && data.length > 0) {
    for (let i = 0, l = data.length; i < l; i++) {
      const item = data[i];
      if (item.type !== 3) {
        if (item.children && item.children.length > 0) {
          const children = formatLimitTree(item.children);
          if (children.length === 0) {
            item.children = null;
          } else {
            item.children = children;
          }
        }
        newData.push(item);
      }
    }
  }
  return newData;
};

export const createFindByIDsFunc = () => {
  const checkedIds: Array<number> = [];
  function pushAllIDs(data: any) {
    for (const item of data) {
      checkedIds.push(item.id);
      if (item.children) {
        pushAllIDs(item.children);
      }
    }
  }
  const findByIDs = (data: any, ids: any) => {
    for (const item of data) {
      if (ids.includes(item.id)) {
        checkedIds.push(item.id);
        if (item.children) {
          pushAllIDs(item.children);
        }
      } else {
        if (item.children) {
          findByIDs(item.children, ids);
        }
      }
    }
    return checkedIds;
  };
  return findByIDs;
};

//平铺树形数据  用于筛选获取name
export const tileTreeNodes = (data: Array<any>) => {
  const newNode: any = [];
  const dep = (data: Array<any>) => {
    for (let i = 0; i < data.length; i++) {
      let item = data[i];
      if (item.children) {
        dep(item.children);
        // break;
      }
      newNode.push(item);
    }
  };
  dep(data);
  return newNode;
};

export enum UserType {
  ADMIN = 1, //admin账号
  MANAGE = 2, //管理员
  NORMAL = 3, //普通用户
}

/**
 * 获取用户类型 0 admin 1 普通管理员 2 普通用户
 */
export const getOrgType = () => {
  const userString = localStorage.getItem('user');
  const user = userString ? JSON.parse(userString) : {};
  //0 admin 1 普通管理员 2 普通用户
  let orgType = 0; //默认admin
  if (user?.type !== UserType.ADMIN) {
    orgType = 1;
  }
  return orgType;
};
